package com.lx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


// @RestController :  @ResponseBody + @Controller 两个注解的作用，直接返回的是字符串，不走视图解析器
@Controller
public class JsonController {

    // @ResponseBody不会走视图解析器，配合@Controller一起使用，直接返回的是字符串
    @RequestMapping("/json1")
    @ResponseBody
    public String json() throws JsonProcessingException {
        User user = new User(1, "乐幼", 3);
        //使用jackson将user转成json格式数据返回给前端
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(user);
    }

    //json返回一个user集合
    @RequestMapping("/json2")
    @ResponseBody
    public String json2() throws JsonProcessingException {
        User user1 = new User(1, "乐幼1",3);
        User user2 = new User(2, "乐幼2",3);
        User user3 = new User(3, "乐幼3",3);
        User user4 = new User(4, "乐幼4",3);
        //将四个对象放入List集合
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        return new ObjectMapper().writeValueAsString(users);
    }

    @RequestMapping("/json3")
    @ResponseBody
    public String json3() {
        User user1 = new User(1, "乐幼1", 3);
        User user2 = new User(2, "乐幼2", 3);
        User user3 = new User(3, "乐幼3", 3);
        User user4 = new User(4, "乐幼4", 3);
        //将四个对象放入List集合
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        return JSON.toJSONString(users);
    }

    @RequestMapping("/json4")
    @ResponseBody
    public String json4() {
        User user1 = new User(1, "乐幼1", 3);
        User user2 = new User(2, "乐幼2", 3);
        User user3 = new User(3, "乐幼3", 3);
        User user4 = new User(4, "乐幼4", 3);
        //将四个对象放入List集合
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);

        //将集合转为json字符串
        String jsonString = JSON.toJSONString(users);
        System.out.println(jsonString);

        //将对象转成json字符串
        String jsonString1 = JSON.toJSONString(new User(1, "乐幼", 8));
        System.out.println(jsonString1);

        //将json数组字符串转成java集合对象
        List<User> userList = JSON.parseArray(jsonString, User.class);
        for (User user : userList) {
            System.out.println(user);
        }

        //将json字符串转成java对象
        User user = JSON.parseObject(jsonString1, User.class);
        System.out.println(user);

        //将java对象转成json对象
        Object jsonObject = JSON.toJSON(user);
        System.out.println(jsonObject);

        return "Hello Fastjson!!!";
    }
}
